from django.db import models


# Create your models here.


def get_default_pfmea_table_info():
    # return "N15 FR&N15A FR&N15A RWD"
    return pfmea_table_info.objects.get(partName="N15 FR&N15A FR&N15A RWD")


class pfmea_table_info(models.Model):
    # 所属区域信息（基地+区域）
    base = models.CharField(max_length=200, null=True, blank=True, verbose_name="所属基地")
    base_zone = models.CharField(max_length=200, null=True, blank=True, verbose_name="所属基地的区域")
    # pfmea文件表头信息
    pfmea_number = models.CharField(max_length=200, null=True, blank=True, verbose_name="PFMEA编号")
    partNumber = models.TextField(null=True, blank=True, verbose_name="零件编号")
    # 设置partName为主键
    partName = models.CharField(primary_key=True, max_length=200, blank=True, verbose_name="机型(零件名称)")

    responsibility = models.CharField(max_length=200, null=True, blank=True, verbose_name="工艺责任")
    keyDate = models.CharField(max_length=200, null=True, blank=True, verbose_name="关键日期")
    editDate = models.CharField(max_length=200, null=True, blank=True, verbose_name="更新日期")

    editor = models.CharField(max_length=200, null=True, blank=True, verbose_name="编制人")
    coreTeam = models.CharField(max_length=200, null=True, blank=True, verbose_name="核心小组")
    version = models.CharField(max_length=200, null=True, blank=True, verbose_name="PFMEA版本号")

    class Meta:
        verbose_name = verbose_name_plural = "PFMEA数据表头信息"

    def __str__(self):
        return self.partName


class pfmea_table_data(models.Model):
    # 所属区域信息（基地+区域）
    base = models.CharField(max_length=200, null=True, blank=True, verbose_name="所属基地")
    base_zone = models.CharField(max_length=200, null=True, blank=True, verbose_name="所属基地的区域")
    # pfmea条目所属工位信息
    pfmea_op = models.CharField(max_length=200, null=True, blank=False, verbose_name="pfmea条目所属工位(pfmea_op)")
    # pfmea条目信息
    ProcessFunction = models.TextField(null=True, blank=True, verbose_name="过程步骤/功能(ProcessFunction)")
    PotentialFailureMode = models.TextField(null=True, blank=True, verbose_name="潜在失效模式(PotentialFailureMode)")
    PotentialEffectsOfFailure = models.TextField(null=True, blank=True, verbose_name="潜在失效后果(PotentialEffectsOfFailure)")
    Severity = models.CharField(max_length=200, null=True, blank=True, verbose_name="严重度S(Severity)")
    Class = models.CharField(max_length=200, null=True, blank=True, verbose_name="分类(Class)")
    PotentialCausesOfFailure = models.TextField(null=True, blank=True, verbose_name="潜在失效原因(PotentialCausesOfFailure)")
    Occurence = models.CharField(max_length=200, null=True, blank=True, verbose_name="频度O(Occurence)")
    CurrentProcessControlsPrevention = models.TextField(null=True, blank=True, verbose_name="控制计划的预防(CurrentProcessControlsPrevention)")
    CurrentProcessControlsDetection = models.TextField(null=True, blank=True, verbose_name="控制计划的探测(CurrentProcessControlsDetection)")

    Detection = models.CharField(max_length=200, null=True, blank=True, verbose_name="探测度D(Detection)")
    Rpn = models.CharField(max_length=200, null=True, blank=True, verbose_name="风险顺序数(RPN)")
    SeverZone = models.CharField(max_length=200, null=True, blank=True, verbose_name="严重区(SeverZone)")
    DetectZone = models.CharField(max_length=200, null=True, blank=True, verbose_name="探测区(DetectZone)")
    RiskPriorLevel = models.CharField(max_length=200, null=True, blank=True, verbose_name="RPL(RiskPriorLevel)")
    RecommendedActions = models.TextField(null=True, blank=True, verbose_name="建议措施(RecommendedActions)")
    ResponsibilityAndTargetCompletionDate = models.CharField(max_length=200, null=True, blank=True, verbose_name="责任人和目标完成日期(ResponsibilityAndTargetCompletionDate)")
    ActionTaken = models.TextField(null=True, blank=True, verbose_name="实施对策(ActionTaken)")
    ActionTakenSeverity = models.CharField(max_length=200, null=True, blank=True, verbose_name="措施后—严重度S(ActionTakenSeverity)")
    ActionTakenOccurence = models.CharField(max_length=200, null=True, blank=True, verbose_name="措施后—频度O(ActionTakenOccurence)")
    ActionTakenDetection = models.CharField(max_length=200, null=True, blank=True, verbose_name="措施后—探测度D(ActionTakenDetection)")
    ActionTakenRpn = models.CharField(max_length=200, null=True, blank=True, verbose_name="措施后—RPN(ActionTakenRpn)")

    # pfmea数据的创建和更新时间
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间(created_at)")
    updated_at = models.DateTimeField(auto_now_add=True, verbose_name="更新时间(updated_at)")

    # 创建外键
    pfmea_info = models.ForeignKey(pfmea_table_info, default=get_default_pfmea_table_info, on_delete=models.CASCADE, verbose_name="关联表头信息(pfmea_info)")

    class Meta:
        verbose_name = verbose_name_plural = "PFMEA表格数据"

    def __str__(self):
        return self.ProcessFunction
